package com.allwees.bs.task.order.notify;


import cn.hutool.core.collection.CollectionUtil;
import com.allwees.bs.core.model.order.config.ScheduleTimeProperties;
import com.allwees.bs.module.mms.dao.RetailerDao;
import com.allwees.bs.module.mms.entity.Retailer;
import com.allwees.bs.module.oms.dao.OmsOrderItemDao;
import com.allwees.bs.module.oms.entity.OmsOrderItem;
import com.allwees.bs.module.oms.service.GeneralOmsOrderItemService;
import com.allwees.bs.module.oms.service.GeneralOmsOrderService;
import com.allwees.core.common.constant.EStatus;
import com.allwees.sender.sms.SmsSender;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service

public class OrderUnShippedRefundTask {


    @Autowired
    private ScheduleTimeProperties scheduleTimeProperties;

    @Autowired
    private OmsOrderItemDao omsOrderItemDao;

    @Autowired
    private RetailerDao retailerDao;

    @Autowired
    private SmsSender smsSender;

    @Autowired
    protected GeneralOmsOrderItemService generalOmsOrderItemService;

    @Autowired
    protected GeneralOmsOrderService generalOmsOrderService;


    @XxlJob(value = "orderUnShippedRefundTask")
    public ReturnT<String> run(String s) {
        doTask();
        return ReturnT.SUCCESS;
    }

    private void doTask() {
        Integer unShippedRefundNotifySecond = scheduleTimeProperties.getUnShippedRefundNotifySecond();
        if (null == unShippedRefundNotifySecond) {
            return;
        }
        log.info("===========[orderUnShippedRefundTask start]===========");
        List<OmsOrderItem> omsOrderItems = omsOrderItemDao.findUnShippedUuids(EStatus.OMS_WAIT_MERCHANT_SHIP.value(),unShippedRefundNotifySecond);
        if(CollectionUtil.isEmpty(omsOrderItems)){
            log.info("===========[orderUnShippedRefundTask end, oms_un_shipped order not find]===========");
            return;
        }

        Map<String, List<OmsOrderItem>> map = omsOrderItems.stream().collect(Collectors.groupingBy(OmsOrderItem::getRetailerUuid,Collectors.toList()));
        for(Map.Entry<String, List<OmsOrderItem>> entry:map.entrySet()) {
            if(entry.getKey().equals("81a650c6ecec11e983ec0242ac110007")){//re02店铺不退款
                  continue;
            }
            Retailer retailer = retailerDao.findByUuid(entry.getKey());
            String mobile = retailer.getPhone();
            if (StringUtils.isBlank(mobile)) {
                log.info("Retailer [{}] phone is null,Retailer uuid:{}", retailer.getCompanyName(),retailer.getUuid());
                continue;
            }
            List<OmsOrderItem> list = entry.getValue();
            log.info(">>> retailerUuid:{}, un_shipped order items,size={}",retailer.getUuid(),list.size());
            for(OmsOrderItem omsOrderItem :list) {
                generalOmsOrderItemService.doRefundedByPlatform(omsOrderItem);
                generalOmsOrderService.doRefundedByRetailer(omsOrderItem.getOrderUuid(),omsOrderItem.getUuid());
            }
            smsSender.merchantUnShippedRefundNotifySendSms(mobile,list.size());
        }
        log.info("===========[orderUnShippedRefundTask end]===========");
    }

}
